# Create your views here.
from django.contrib.auth.decorators import login_required
from django.core.context_processors import csrf
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.gis.geos import Point
from tibus.forms import FormularioParada
from tibus.models import Parada,  Recorrido

@login_required
def index(request):
    return render_to_response('index.html', {'usuario': request.user,})
    
def recorrido(request):
    c = {}
    c.update(csrf(request))
    listaParadas = Parada.objects.all()
    if request.method == 'POST':
        form = FormularioParada(request.POST)
        if form.is_valid():
            if request.POST.get('accion') == 'addParada':
                parLin = Recorrido.objects.get(linea=request.POST.get('linea'))
                parOrden = int(request.POST.get('orden'))
                parLat = float(request.POST.get('newlatitud'))
                parLon = float(request.POST.get('newlongitud'))
                newParada = Parada(orden = parOrden,  latitud = parLat, longitud = parLon, coordenadas = Point(parLat, parLon), linea = parLin)  
                newParada.save()
        return render_to_response('recorrido.html', {'form': form,  'listaParadas': listaParadas}, context_instance=RequestContext(request))
    else:
        form = FormularioParada()
    return render_to_response('recorrido.html', {'form': form,  'listaParadas': listaParadas}, context_instance=RequestContext(request))
   
def modelo(request):
    return render_to_response('modelo.html')
    
def prediccion(request):
    return render_to_response('prediccion.html')
    
def ayuda(request):
    return render_to_response('ayuda.html')

#return HttpResponse("Hello, world. You're at the poll index.")
